/**
* 
* Copyright [2007] [Sunny Liu]
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package ${appConfig.outputPackageName}.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import ${appConfig.outputPackageName}.web.view.FlexXMLView;
import ${appConfig.outputPackageName}.controller.ServerProcessStatus;
import ${appConfig.outputPackageName}.controller.ActionBean;
import ${appConfig.outputPackageName}.trans.TransactionProxy;
import ${appConfig.outputPackageName}.model.${entity.javaName};

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class  ${entity.javaName}AddController extends AbstractController 
{
	private static Logger log = LoggerFactory.getLogger(${entity.javaName}AddController.class);
	private TransactionProxy transactionProxy;	
	
	
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
	HttpServletResponse response) throws Exception 
	{
		if(log.isDebugEnabled()) log.debug("Entering ${entity.javaName}AddController ");		
		ServerProcessStatus status = new ServerProcessStatus();	
		ActionBean action = new ActionBean();
		BeanUtils.populate(action, request.getParameterMap());
		${entity.javaName} commandObject = new ${entity.javaName}();
		if(!hasAllRequiredFields(request)){
			status.setOutput(commandObject);
		}else{									
			BeanUtils.populate(commandObject, request.getParameterMap());		
			status.setAction(action.getAction());
			try{
				commandObject =  getTransactionProxy().add${entity.javaName}(commandObject);		
				if(commandObject!=null){
					status.setMessage("${entity.javaName} added to database successfully.");
					status.setOutput(commandObject);
				}else{
					status.setMessage("failed to add ${entity.javaName} to database .");
				}
				
			}catch(Exception ex){
				log.error("Failed to execute add()", ex);
				status.setError(ex);
				status.setMessage("failed to add ${entity.javaName} to database .");
			}	
		}
		String viewName = getViewName();
		if(action.isXmlView()){
			viewName = FlexXMLView.NAME;
		}		
		ModelAndView mv = new ModelAndView(viewName);
		mv.addObject(ServerProcessStatus.SERVER_PROCESS_STATUS, status);
		if(log.isDebugEnabled()) log.debug("Exiting ${entity.javaName}AddController ");		
		return mv;
	}
	
	protected boolean hasAllRequiredFields(HttpServletRequest request)
	{
		String[] paramNames = new String[]{<#list entity.primaryKeyColumns as attribute>"${attribute.javaName}"<#if attribute_has_next>, </#if></#list>};
		for(int i=0; i<paramNames.length; i++)
		{
			if(request.getParameter(paramNames[i])==null){
				return false;
			}
		}
		return true;
	}
	
	public TransactionProxy getTransactionProxy()
	{
		return this.transactionProxy;
	}
	
	public void setTransactionProxy(TransactionProxy proxy)
	{
		this.transactionProxy = proxy;
	}
	
	protected String getViewName()
	{
		return "${entity.javaName}AddView";
	}
	
}